package com.android.hedgehog;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.android.hedgehog.framework.FileIO;

/**
 * Stores the settings and loads/saves them.
 * 
 * 
 */
public class Settings {
	public static boolean soundEnabled = true;
	public static String userName = "";
	public static int age = 0;
	public static boolean isMale = true;
	public static int round = 1; //test, should be 0 or 1
	public static int cyberScore;
	public static int globalRank = 0;
	public static int iqRating = 0;
	public static final int totalPuzzles = 8;
	public static final int totalRounds = 8;
	public static int[][] highScores = new int[totalPuzzles][5];
	public static int[] roundScores = new int[totalRounds];

	/*
	 * static method "Load" loads settings from a file called .cyberiq from the
	 * external storage. a fileIO instance needed for that. This assumes that
	 * the "sound" & each "high-score" are stored separately on a different
	 * line.
	 * 
	 * if external storage is not available or there is no settings yet, then
	 * use defaults and ignore failure.
	 */
	public static void load(FileIO files) {
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(
					files.readFile(".cyberiq")));
			soundEnabled = Boolean.parseBoolean(in.readLine());
			userName = in.readLine();
			age = Integer.parseInt(in.readLine());
			isMale = Boolean.parseBoolean(in.readLine());
			round = Integer.parseInt(in.readLine());
			cyberScore = Integer.parseInt(in.readLine());
			globalRank = Integer.parseInt(in.readLine());
			iqRating = Integer.parseInt(in.readLine());

			for (int i = 0; i < totalPuzzles; i++) {// load Scores
				for (int j = 0; j < 5; j++) {
					highScores[i][j] = Integer.parseInt(in.readLine());
				}
			}// LOAD SCORES
			for (int i = 0; i < totalRounds; i++) {
				roundScores[i] = Integer.parseInt(in.readLine());
			}

		} catch (IOException e) {
			// :( It's ok we have defaults
		} catch (NumberFormatException e) {
			// :/ It's ok, defaults save our day
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
			}
		}
	}

	/**
	 * Takes the current "settings" and serializes them to .cyberiq file on
	 * external storage. sound and each high score on separate line.
	 * 
	 */
	public static void save(FileIO files) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(
					files.writeFile(".cyberiq")));
			out.write(Boolean.toString(soundEnabled));
			out.write("\n");
			out.write(userName);
			out.write("\n");
			out.write(Integer.toString(age));
			out.write("\n");
			out.write(Boolean.toString(isMale));
			out.write("\n");
			out.write(Integer.toString(round));
			out.write("\n");

			out.write(Integer.toString(cyberScore));
			out.write("\n");
			out.write(Integer.toString(globalRank));
			out.write("\n");
			out.write(Integer.toString(iqRating));
			out.write("\n");

			for (int i = 0; i < totalPuzzles; i++) {// save Scores
				for (int j = 0; j < 5; j++) {
					out.write(Integer.toString(highScores[i][j]));
					out.write("\n");
				}
			}// Save SCORES

			for (int i = 0; i < totalRounds; i++) {// save round SCORES
				out.write(Integer.toString(roundScores[i]));
				out.write("\n");
			}// save round SCORES

		} catch (IOException e) {
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
			}
		}
	}

	public static void addScore(int gameNumber, int score) {
		for (int i = 0; i < 5; i++) {
			if (highScores[gameNumber][i] < score) {
				for (int j = 4; j > i; j--)
					highScores[gameNumber][j] = highScores[gameNumber][j - 1];
				highScores[gameNumber][i] = score;
				break;
			}
		}
		updateCyberScore();
	}

	public static void addRoundScore(int roundNumber, int score) {
		if (roundScores[roundNumber] < score) {
			roundScores[roundNumber] = score;
			updateCyberScore();
		}
	}

	private static void updateCyberScore() {
		float sum = 0;
		float sum2 = 0;
		int counter = 0;
		int counter2 = 0;

		for (int i = 0; i < totalPuzzles; i++) {
			for (int j = 0; j < 5; j++) {
				if (highScores[i][j] > 0) {
					sum += highScores[i][j];
					counter++;

				}
			}
		}

		for (int i = 0; i < totalRounds; i++) {
			if (roundScores[i] > 0) {
				sum2 += roundScores[i];
				counter2++;
			}
		}
		cyberScore = Math.round(sum + sum2);
		iqRating = (Math.round((sum / counter) + (sum2 / counter2)));

	}



}
